/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.datetime;

import org.openaecfm.model.measure.Boolean;
import org.openaecfm.model.datetime.ResourceTime;
import org.openaecfm.model.datetime.SchedulingTime;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class ResourceTimeIO extends
		AbstractEntityInstanceInputImpl<ResourceTime> implements
		IEntityInstanceOutput<ResourceTime> {

	private IEntityInstanceUtilOutput<SchedulingTime> utilOutput;
	private IEntityInstanceUtilInput<SchedulingTime> utilInput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<SchedulingTime> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(IEntityInstanceUtilInput<SchedulingTime> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public String exportSPF(ResourceTime pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCRESOURCETIME",
				appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set ScheduleWork
		if (pojo.getScheduleWork() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getScheduleWork(),
					StringParameter.UTF_8));
		}

		// Set ScheduleUsage
		if (pojo.getScheduleUsage() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getScheduleUsage()));
		}

		// Set ScheduleStart
		if (pojo.getScheduleStart() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getScheduleStart(),
					StringParameter.UTF_8));
		}

		// Set ScheduleFinish
		if (pojo.getScheduleFinish() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getScheduleFinish(),
					StringParameter.UTF_8));
		}

		// Set ScheduleContour
		if (pojo.getScheduleContour() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getScheduleContour(),
					StringParameter.UTF_8));
		}

		// Set LevelingDelay
		if (pojo.getLevelingDelay() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getLevelingDelay(),
					StringParameter.UTF_8));
		}

		// Set IsOverAllocated
		if (pojo.getIsOverAllocated() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EnumerationParameter(pojo.getIsOverAllocated()));
		}

		// Set StatusTime
		if (pojo.getStatusTime() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getStatusTime(),
					StringParameter.UTF_8));
		}

		// Set ActualWork
		if (pojo.getActualWork() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getActualWork(),
					StringParameter.UTF_8));
		}

		// Set ActualUsage
		if (pojo.getActualUsage() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getActualUsage()));
		}

		// Set ActualStart
		if (pojo.getActualStart() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getActualStart(),
					StringParameter.UTF_8));
		}

		// Set ActualFinish
		if (pojo.getActualFinish() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getActualFinish(),
					StringParameter.UTF_8));
		}

		// Set RemainingWork
		if (pojo.getRemainingWork() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getRemainingWork(),
					StringParameter.UTF_8));
		}

		// Set RemainingUsage
		if (pojo.getRemainingUsage() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getRemainingUsage()));
		}

		// Set Completion
		if (pojo.getCompletion() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getCompletion()));
		}

		return ei.toString();
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei, ResourceTime pojo,
			IAppDataManager appDataManager) {
		return;
	}

	@Override
	protected ResourceTime doCreateObject() {
		return new ResourceTime();
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			ResourceTime pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set ScheduleWork
		Parameter scheduleWork = sei.getParameter(3);
		if (scheduleWork.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) scheduleWork;
			pojo.setScheduleWork(s.toUTF8());
		}

		// Set ScheduleUsage
		Parameter scheduleUsage = sei.getParameter(4);
		if (scheduleUsage.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) scheduleUsage;
			pojo.setScheduleUsage(r.toDouble());
		}

		// Set ScheduleStart
		Parameter scheduleStart = sei.getParameter(5);
		if (scheduleStart.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) scheduleStart;
			pojo.setScheduleStart(s.toUTF8());
		}

		// Set ScheduleFinish
		Parameter scheduleFinish = sei.getParameter(6);
		if (scheduleFinish.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) scheduleFinish;
			pojo.setScheduleFinish(s.toUTF8());
		}

		// Set ScheduleContour
		Parameter scheduleContour = sei.getParameter(7);
		if (scheduleContour.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) scheduleContour;
			pojo.setScheduleContour(s.toUTF8());
		}

		// Set LevelingDelay
		Parameter levelingDelay = sei.getParameter(8);
		if (levelingDelay.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) levelingDelay;
			pojo.setLevelingDelay(s.toUTF8());
		}

		// Set IsOverAllocated
		Parameter isOverAllocated = sei.getParameter(9);
		if (isOverAllocated.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) isOverAllocated;
			pojo.setIsOverAllocated(Boolean.valueOf(e.toEnumString()));
		}

		// Set StatusTime
		Parameter statusTime = sei.getParameter(10);
		if (statusTime.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) statusTime;
			pojo.setStatusTime(s.toUTF8());
		}

		// Set ActualWork
		Parameter actualWork = sei.getParameter(11);
		if (actualWork.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) actualWork;
			pojo.setActualWork(s.toUTF8());
		}

		// Set ActualUsage
		Parameter actualUsage = sei.getParameter(12);
		if (actualUsage.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) actualUsage;
			pojo.setActualUsage(r.toDouble());
		}

		// Set ActualStart
		Parameter actualStart = sei.getParameter(13);
		if (actualStart.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) actualStart;
			pojo.setActualStart(s.toUTF8());
		}

		// Set ActualFinish
		Parameter actualFinish = sei.getParameter(14);
		if (actualFinish.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) actualFinish;
			pojo.setActualFinish(s.toUTF8());
		}

		// Set RemainingWork
		Parameter remainingWork = sei.getParameter(15);
		if (remainingWork.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) remainingWork;
			pojo.setRemainingWork(s.toUTF8());
		}

		// Set RemainingUsage
		Parameter remainingUsage = sei.getParameter(16);
		if (remainingUsage.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) remainingUsage;
			pojo.setRemainingUsage(r.toDouble());
		}

		// Set Completion
		Parameter completion = sei.getParameter(17);
		if (completion.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) completion;
			pojo.setCompletion(r.toDouble());
		}
	}
}
